package com.wangxiang.third.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.internet.MimeMessage;
import javax.naming.Context;
import java.util.HashMap;

@RestController
public class MailController {
    @Autowired
    private JavaMailSender javaMailSender;

    @RequestMapping("mail")
    public boolean mail(@RequestBody HashMap<String , String> map){
        System.out.println("请求信息"+map);
        //目标邮箱，验证码
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("2293672863@qq.com");
        message.setTo(map.get("toEmail"));
        message.setSubject("王相的毕业设计小程序客户端验证码");
        message.setText("验证码："+map.get("captcha")+"--------"+"失效时间5分钟");
        try {
            javaMailSender.send(message);
            return true;
        }catch (Exception e){
            System.out.println("异常"+e);
            return false;
        }
    }
    @RequestMapping("mail/test")
    public boolean mailTest(@RequestBody HashMap<String , Object> map){
        System.out.println("请求信息"+map);
        //目标邮箱，验证码
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("2293672863@qq.com");
        message.setTo(map.get("toEmail").toString());
        message.setSubject("王相的毕业设计管理后台管理员");
        message.setText("账号："+map.get("loginId")+
                "\n"+"密码："+map.get("password")+
                "\n"+"添加时间："+map.get("time")+
                "\n"+"系统角色："+map.get("role")+"\n");
        try {
            javaMailSender.send(message);
            return true;
        }catch (Exception e){
            System.out.println("异常"+e);
            return false;
        }
    }
    @RequestMapping("email/audit")
    public boolean mailaudit(@RequestBody HashMap<String , Object> map){
        //目标邮箱，验证码
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("2293672863@qq.com");
        System.out.println(map.get("toEmail"));
        message.setTo(map.get("toEmail").toString());
        message.setSubject("王相的毕业设计管理后台管理员");
        message.setText("你正在注册成为王相毕设设计管理程序的管理员");
        try {
            javaMailSender.send(message);
            return true;
        }catch (Exception e){
            System.out.println("异常"+e);
            return false;
        }
    }
    @RequestMapping("email/update/bing")
    public boolean emailText(@RequestBody HashMap<String , Object> map){
        //目标邮箱，验证码
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("2293672863@qq.com");
        System.out.println(map.get("toEmail"));
        message.setTo(map.get("toEmail").toString());
        message.setSubject(map.get("title").toString());
        message.setText(map.get("text").toString());
        try {
            javaMailSender.send(message);
            return true;
        }catch (Exception e){
            System.out.println("异常"+e);
            return false;
        }
    }

}
